package org.example.hadoop.service.priceScopeFrequency;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author wwy
 * @date 2020-11-10 21:04
 */
public class PriceScopeFrequencyMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
    private static final IntWritable valueInfo = new IntWritable(1);
    /**
     * k1     v1
     *        https://book.douban.com/subject/4913064/ 活着 小说 余华 9.4 531574 20.00元 作家出版社 2012-8-1 132697
     *
     * k2     v2
     *
     * 20     1
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] lineArray = line.split(" ");
        String price = lineArray[6];
        if(price.length()<=5){
            return;
        }
        if(!price.contains(".")){
            return;
        }
        String priceStr = price.substring(0, price.indexOf("."));
        int priceInt = 0;
        try {
            priceInt = (int) Double.parseDouble(priceStr);
            context.write(new IntWritable(priceInt),valueInfo);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }


    }
}
